# WorkflowCore工作流功能测试脚本
Write-Host "🚀 开始WorkflowCore工作流审批功能测试" -ForegroundColor Green
Write-Host "==================================================" -ForegroundColor Yellow

$baseUrl = "http://localhost:8080"

try {
    # 测试登录
    Write-Host "🔐 测试登录..." -ForegroundColor Cyan
    
    $loginData = @{
        username = "admin"
        password = "1q2w3E*"
    } | ConvertTo-Json
    
    $headers = @{
        "Content-Type" = "application/json"
    }
    
    $loginResponse = Invoke-RestMethod -Uri "$baseUrl/api/account/login" -Method POST -Body $loginData -Headers $headers
    
    if ($loginResponse.success) {
        Write-Host "✅ 登录成功" -ForegroundColor Green
        
        $token = $loginResponse.accessToken
        $authHeaders = @{
            "Authorization" = "Bearer $token"
            "Content-Type" = "application/json"
        }
        
        # 测试获取工作流实例列表
        Write-Host "`n📋 测试获取工作流实例列表..." -ForegroundColor Cyan
        try {
            $instancesResponse = Invoke-RestMethod -Uri "$baseUrl/api/app/workflow/instances" -Method GET -Headers $authHeaders
            Write-Host "✅ 工作流实例列表获取成功" -ForegroundColor Green
            Write-Host "实例数量: $($instancesResponse.data.Count)" -ForegroundColor Yellow
        }
        catch {
            Write-Host "❌ 获取工作流实例列表失败: $($_.Exception.Message)" -ForegroundColor Red
        }
        
        # 测试获取待审批列表
        Write-Host "`n⏳ 测试获取待审批列表..." -ForegroundColor Cyan
        try {
            $pendingResponse = Invoke-RestMethod -Uri "$baseUrl/api/app/workflow/pending-approvals" -Method GET -Headers $authHeaders
            Write-Host "✅ 待审批列表获取成功" -ForegroundColor Green
            Write-Host "待审批数量: $($pendingResponse.data.Count)" -ForegroundColor Yellow
        }
        catch {
            Write-Host "❌ 获取待审批列表失败: $($_.Exception.Message)" -ForegroundColor Red
        }
        
        # 测试创建入库单
        Write-Host "`n📦 测试创建入库单..." -ForegroundColor Cyan
        $inboundData = @{
            code = "IN$(Get-Date -Format 'yyyyMMddHHmmss')"
            name = "测试入库单"
            price = 1000.00
            state = 2
            remark = "WorkflowCore测试入库单"
        } | ConvertTo-Json
        
        try {
            $inboundResponse = Invoke-RestMethod -Uri "$baseUrl/api/app/inboundmanagement/create" -Method POST -Body $inboundData -Headers $authHeaders
            Write-Host "✅ 入库单创建成功" -ForegroundColor Green
            Write-Host "入库单ID: $($inboundResponse.data)" -ForegroundColor Yellow
        }
        catch {
            Write-Host "❌ 入库单创建失败: $($_.Exception.Message)" -ForegroundColor Red
        }
        
        # 测试工作流状态
        Write-Host "`n🔍 测试工作流状态查询..." -ForegroundColor Cyan
        try {
            $statusResponse = Invoke-RestMethod -Uri "$baseUrl/api/app/workflow/status/test-workflow-id" -Method GET -Headers $authHeaders
            Write-Host "✅ 工作流状态查询成功" -ForegroundColor Green
        }
        catch {
            Write-Host "❌ 工作流状态查询失败: $($_.Exception.Message)" -ForegroundColor Red
        }
    }
    else {
        Write-Host "❌ 登录失败: $($loginResponse.message)" -ForegroundColor Red
    }
}
catch {
    Write-Host "❌ 测试异常: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host "`n==================================================" -ForegroundColor Yellow
Write-Host "✅ WorkflowCore工作流审批功能测试完成" -ForegroundColor Green 